package com.bilibili.juc.cf.one;

import cn.hutool.core.util.RandomUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class FutureExceptionTwoTest {

    public static void main(String[] args) throws Exception {

        CompletableFuture<FutureResultVO> orgFuture = CompletableFuture.supplyAsync(
                ()->{
                    System.out.println("当前线程名称：" + Thread.currentThread().getName());
                    int count = RandomUtil.randomInt(1, 10);
                    System.out.println("随机数为: " + count);
                    if (count < 7){
                        throw new RuntimeException();
                    }else {
                        return FutureResultVO.success("CompletableFuture正常运行返回结果");
                    }

                }
        );
/*
orgFuture.exceptionally方法任务：当orgFuture产生异常才会执行exceptionally方法。并且会返回一个指定的值。
如果没有继续抛出异常这个异常信息就会被吃掉。当然了。也可以根据返回值确定是否异常了。正常处理流程，异常处理流程。这个比较优雅
 */
        CompletableFuture<FutureResultVO> exceptionFuture = orgFuture.exceptionally((e) -> {
            e.printStackTrace();
            return FutureResultVO.fail("你的程序异常啦",e);
        });

        FutureResultVO futureResultVO = exceptionFuture.get();
        if (futureResultVO.isSuccess()){
            System.out.println("成功==>> " + futureResultVO);
        }else {
            System.out.println("### 失败==>> " + futureResultVO);
        }
        for (int i = 0 ; i < 5;i++){
            System.out.println(" 主程序正在执行其它的任务"+i);
            TimeUnit.SECONDS.sleep(1);
        }
    }
    /*
正常情况的运行结果:
当前线程名称：ForkJoinPool.commonPool-worker-1
随机数为: 3
CompletableFuture正常运行返回结果
================================================================================================
异常情况的运行结果:
当前线程名称：ForkJoinPool.commonPool-worker-1
随机数为: 9
你的程序异常啦
java.util.concurrent.CompletionException: java.lang.RuntimeException
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:320)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1770)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1760)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:373)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1182)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1655)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1622)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
Caused by: java.lang.RuntimeException
	at com.bilibili.juc.cf.one.FutureExceptionTest.lambda$main$0(FutureExceptionTest.java:18)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
	... 6 more
     */
}
